package com.tencent.sr.iris.basic.saas.common.enums;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.comparator.CompareUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 对外状态 0：一阶进行中，1：一阶审核中，2：一阶已完成 3：一阶未完成 4：二阶进行中 5：二阶审核中 6：二阶已完成 7：二阶未完成
 */
@Deprecated
@AllArgsConstructor
@Getter
public enum OutTaskStatusEnum {
    FIRST_PROGRESS(0, Arrays.asList(UserTaskStatusEnum.PROGRESS.getCode(), UserTaskStatusEnum.PROGRESS.getCode()),
            Arrays.asList(TaskEstimateStatusEnum.PROGRESS.getCode(), TaskEstimateStatusEnum.PROGRESS.getCode()), "一阶进行中"),

    FIRST_AUDITING(1, Arrays.asList(UserTaskStatusEnum.PROGRESS.getCode(), UserTaskStatusEnum.PROGRESS.getCode()),
            Arrays.asList(TaskEstimateStatusEnum.FINISHED.getCode(), TaskEstimateStatusEnum.PROGRESS.getCode()), "一阶审核中"),

    FIRST_AUDITING_1(1, Arrays.asList(UserTaskStatusEnum.PROGRESS.getCode(), UserTaskStatusEnum.PROGRESS.getCode()),
            Arrays.asList(TaskEstimateStatusEnum.FINISHED.getCode(), TaskEstimateStatusEnum.FINISHED.getCode()), "一阶审核中"),

    FIRST_AUDITING_2(1, Arrays.asList(UserTaskStatusEnum.PROGRESS.getCode(), UserTaskStatusEnum.UNFINISHED.getCode()),
            Arrays.asList(TaskEstimateStatusEnum.FINISHED.getCode(), TaskEstimateStatusEnum.UNFINISHED.getCode()), "一阶审核中"),

    //状态列表同SECOND_PROGRESS
    FIRST_FINISHED(2, Arrays.asList(UserTaskStatusEnum.FINISHED.getCode(), UserTaskStatusEnum.PROGRESS.getCode()),
            Arrays.asList(TaskEstimateStatusEnum.FINISHED.getCode(), TaskEstimateStatusEnum.PROGRESS.getCode()), "一阶已完成"),

    //状态列表同SECOND_UNFINISHED
    FIRST_FINISHED_1(2, Arrays.asList(UserTaskStatusEnum.FINISHED.getCode(), UserTaskStatusEnum.UNFINISHED.getCode()),
            Arrays.asList(TaskEstimateStatusEnum.FINISHED.getCode(), TaskEstimateStatusEnum.UNFINISHED.getCode()), "一阶已完成"),

    FIRST_UNFINISHED(3, Arrays.asList(UserTaskStatusEnum.UNFINISHED.getCode(), UserTaskStatusEnum.UNFINISHED.getCode()),
            Arrays.asList(TaskEstimateStatusEnum.UNFINISHED.getCode(), TaskEstimateStatusEnum.UNFINISHED.getCode()), "一阶未完成"),

    SECOND_PROGRESS(4, Arrays.asList(UserTaskStatusEnum.FINISHED.getCode(), UserTaskStatusEnum.PROGRESS.getCode()),
            Arrays.asList(TaskEstimateStatusEnum.FINISHED.getCode(), TaskEstimateStatusEnum.PROGRESS.getCode()), "二阶进行中"),

    SECOND_AUDITING(5, Arrays.asList(UserTaskStatusEnum.FINISHED.getCode(), UserTaskStatusEnum.PROGRESS.getCode()),
            Arrays.asList(TaskEstimateStatusEnum.FINISHED.getCode(), TaskEstimateStatusEnum.FINISHED.getCode()), "二阶审核中"),

    SECOND_FINISHED(6, Arrays.asList(UserTaskStatusEnum.FINISHED.getCode(), UserTaskStatusEnum.FINISHED.getCode()),
            Arrays.asList(TaskEstimateStatusEnum.FINISHED.getCode(), TaskEstimateStatusEnum.FINISHED.getCode()), "二阶已完成"),

    SECOND_UNFINISHED(7, Arrays.asList(UserTaskStatusEnum.FINISHED.getCode(), UserTaskStatusEnum.UNFINISHED.getCode()),
            Arrays.asList(TaskEstimateStatusEnum.FINISHED.getCode(), TaskEstimateStatusEnum.UNFINISHED.getCode()), "二阶未完成"),
    ;

    private final int code;

    private final List<Byte> userTaskStatusList;
    private final List<Byte> estimateUserTaskStatusList;

    private final String desc;

    public static OutTaskStatusEnum getByCode(Integer code) {
        for (OutTaskStatusEnum staff : OutTaskStatusEnum.values()) {
            if (Objects.equals(staff.getCode(), code)) {
                return staff;
            }
        }
        return null;
    }

    public static OutTaskStatusEnum getByStatusList(List<Byte> userTaskStatusList, List<Byte> estimateUserTaskStatusList,
                                                    boolean firstThresholdGreater, boolean secondThresholdGreater) {
        if (CollectionUtil.isEmpty(userTaskStatusList) || CollectionUtil.isEmpty(estimateUserTaskStatusList)
                || userTaskStatusList.size() != estimateUserTaskStatusList.size()) {
            return null;
        }

        for (OutTaskStatusEnum statusEnum : OutTaskStatusEnum.values()) {
            List<Byte> taskStatusList = statusEnum.getUserTaskStatusList();
            List<Byte> estimateStatusList = statusEnum.getEstimateUserTaskStatusList();
            if (userTaskStatusList.size() != taskStatusList.size()
                    || estimateStatusList.size() != estimateUserTaskStatusList.size()) {
                continue;
            }

            boolean allMatch = true;
            for (int i = 0; i < userTaskStatusList.size(); i++) {
                if (CompareUtil.compare(userTaskStatusList.get(i), taskStatusList.get(i)) != 0
                        || CompareUtil.compare(estimateStatusList.get(i), estimateUserTaskStatusList.get(i)) != 0) {
                    allMatch = false;
                }
            }
            if (allMatch) {
                if (Objects.equals(statusEnum, FIRST_FINISHED) && firstThresholdGreater) {
                    return SECOND_PROGRESS;
                }

                if (Objects.equals(statusEnum, FIRST_FINISHED_1) && secondThresholdGreater) {
                    return SECOND_UNFINISHED;
                }
                return statusEnum;
            }
        }

        return null;
    }
}
